﻿using System.IO;
using System.Web.Hosting;
using System.Xml;
using NopSolutions.NopCommerce.BusinessLogic.SEO;
using NopSolutions.NopCommerce.BusinessLogic.Services;

namespace NopSolutions.NopCommerce.BusinessLogic.Infrastructure
{
    public class CategoryXmlCache
    {
        private string _xmlCacheFileName = "categories.config";
        private static CategoryXmlCache _instance;

        public static CategoryXmlCache Instance {
            get {
                if (_instance == null)
                    _instance = new CategoryXmlCache();

                return _instance;
            }
        }

        public string XmlPath {
            get { return HostingEnvironment.MapPath("~/App_Data/" + _xmlCacheFileName); }
        }

        public bool IsXmlGenerated {
            get { return File.Exists(XmlPath); }
        }

        public ICategoryService CategoryService {
            get { return IoC.Resolve<ICategoryService>(); }
        }

        public XmlDocument Build() {
            XmlDocument document = new XmlDocument();
            XmlNode categoriesNode = document.CreateElement("categories");
            document.AppendChild(categoriesNode);

            Build(0, categoriesNode, document);

            document.Save(XmlPath);

            return document;
        }

        public XmlDocument Load() {
            XmlDocument document = null;

            if (!IsXmlGenerated) {
                document = Build();
                return document;
            }

            document = new XmlDocument();
            document.Load(XmlPath);
            return document;
        }

        private void Build(int level, XmlNode rootNode, XmlDocument document) {
            foreach (var category in this.CategoryService.GetAllCategoriesByParentCategoryId(level)) {
                XmlNode categoryNode = document.CreateElement("category");

                XmlAttribute categoryIdAttribute = document.CreateAttribute("categoryId");
                categoryIdAttribute.Value = category.CategoryId.ToString();
                categoryNode.Attributes.Append(categoryIdAttribute);

                XmlAttribute categoryNameAttribute = document.CreateAttribute("categoryName");
                categoryNameAttribute.Value = category.Name;
                categoryNode.Attributes.Append(categoryNameAttribute);

                XmlAttribute categoryUrlAttribute = document.CreateAttribute("categoryUrl");
                categoryUrlAttribute.Value = SEOHelper.GetCategoryUrl(category);
                categoryNode.Attributes.Append(categoryUrlAttribute);

                if (category.ParentCategory != null) {
                    XmlAttribute parentCategoryIdAttribute = document.CreateAttribute("parentCategoryId");
                    parentCategoryIdAttribute.Value = category.ParentCategory.CategoryId.ToString();
                    categoryNode.Attributes.Append(parentCategoryIdAttribute);
                }

                if (category.HasChildren)
                    Build(category.CategoryId, categoryNode, document);

                rootNode.AppendChild(categoryNode);
            }
        }
    }
}
